package com.czq.gulimail.gulimailproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czq.common.utils.PageUtils;
import com.czq.common.utils.Query;
import com.czq.gulimail.gulimailproduct.dao.CategoryDao;
import com.czq.gulimail.gulimailproduct.entity.CategoryEntity;
import com.czq.gulimail.gulimailproduct.service.CategoryBrandRelationService;
import com.czq.gulimail.gulimailproduct.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 多次分类树形查询
     *
     * @return
     */
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        return categoryEntities.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .peek(entry -> entry.setChildren(getChildren(entry, categoryEntities)))
                .sorted(Comparator.comparingInt(entry -> (entry.getSort() == null ? 0 : entry.getSort())))
                .collect(Collectors.toList());
    }

    /**
     * 逻辑删除
     *
     * @param list
     */
    public void deleteByIds(List<Long> list) {
        //TODO 先检查有无其他引用，有其他引用可以删除
        baseMapper.deleteBatchIds(list);
    }

    @Transactional
    @Override
    public void updateCategoryAndRelations(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateByCate(category.getCatId(), category.getName());
    }

    @Transactional
    @Override
    public void deleteCategoryAndRelations(Long[] catIds) {
        this.deleteByIds(Arrays.asList(catIds));
        categoryBrandRelationService.deleteBatchByCateId(catIds);
    }

    /**
     * 求catelogId的父亲以及爷爷
     *
     * @param catelogId
     * @return
     */
    @Override
    public Long[] getCatePath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        CategoryEntity category = this.getById(catelogId);
        getParent(paths, category);

        Collections.reverse(paths); // 或者保留原来的数组反转逻辑，根据需要选择

        // 将列表转换为数组并返回
        return paths.toArray(new Long[0]);
    }

    public void getParent(List<Long> paths, CategoryEntity category) {
        if(category==null) return;
        paths.add(category.getCatId());
        if (category.getParentCid() == 0) return;
        CategoryEntity parent = this.getById(category.getParentCid());
        getParent(paths, parent);
    }


    public List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        return all.stream()
                .filter(entry -> root.getCatId().equals(entry.getParentCid()))
                .peek(entry -> entry.setChildren(getChildren(entry, all)))
                .sorted(Comparator.comparingInt(entry -> (entry.getSort() == null ? 0 : entry.getSort())))
                .collect(Collectors.toList());
    }


}
